import Vue from 'vue'
import Vuex from 'vuex'
import vuexP from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userList:[]
  },
  getters: {
  },
  mutations: {
    // 获取
    setUserList(state,data){
       state.userList = data
    },
    addUser(state,form){
      let time = new Date().getTime()
      state.userList.push({
        ...form,//只有四个字段
        id:time,
        mg_state:false,
        role_name:'管理员',
        create_time:time

      })
      console.log(state.userList);

    },
    editUser(state,form){
      let index = state.userList.findIndex(item=>item.id ==form.id)
      state.userList[index] = {
        ...state.userList[index],
        ...form

      }
    },
    // 修改状态
    handleChange(state,form){
        console.log(form.mg_state);
        let index = state.userList.findIndex(item=>item.id ==form.id)
        state.userList[index].mg_state = form.mg_state
    },
    // 删除
    handleDel(state,id){
        let index = state.userList.findIndex(item=>item.id ==id)
        state.userList.splice(index,1)
    }
  },
  actions: {
  },
  modules: {
  },
  plugins:[vuexP()]
})
